package com.michaelbulava.TFS.vcs.ui;

import com.intellij.openapi.vcs.versionBrowser.ChangeBrowserSettings;
import com.intellij.openapi.vcs.versionBrowser.ChangesBrowserSettingsEditor;
import com.intellij.openapi.vcs.versionBrowser.StandardVersionFilterComponent;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.michaelbulava.TFS.Messages;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by MichaelA on 8/18/2015.
 */
public class TFSVersionFilterComponent extends StandardVersionFilterComponent {
    private JPanel myPanel;
    private JCheckBox myUseAuthorFilter;
    private JTextField myAuthorField;
    private JPanel myStandardPanel;

    public TFSVersionFilterComponent(boolean showDateFilter) {
        super(showDateFilter);
        setupUI();
        myStandardPanel.setLayout(new BorderLayout());
        myStandardPanel.add(getStandardPanel(), "Center");
        init(new ChangeBrowserSettings());
    }

    @Override
    public JComponent getComponent() {
        return myPanel;
    }

    @Override
    protected void updateAllEnabled(ActionEvent e){
        super.updateAllEnabled(e);
        updatePair(myUseAuthorFilter, myAuthorField, e);
    }

    @Override
    protected void initValues(ChangeBrowserSettings settings){
        super.initValues(settings);
        myUseAuthorFilter.setSelected(settings.USE_USER_FILTER);
        myAuthorField.setText(settings.USER);
    }

    @Override
    public void saveValues(ChangeBrowserSettings settings){
        super.saveValues(settings);
        settings.USER = myAuthorField.getText();
        settings.USE_USER_FILTER = myUseAuthorFilter.isSelected();
    }

    @Override
    protected void installCheckBoxListener(ActionListener filterListener){
        super.installCheckBoxListener(filterListener);
        myUseAuthorFilter.addActionListener(filterListener);
        myAuthorField.addActionListener(filterListener);
    }

    public String getAuthorFilter(){
        if (myUseAuthorFilter.isSelected() && myAuthorField.getText().length() > 0)
            return myUseAuthorFilter.getText();
        else
            return null;
    }

    @Override
    protected String getChangeNumberTitle(){
        return Messages.getString("TFSVersionFilterComponent.ChangeNumberTitle");
    }


    private void setupUI(){
        JPanel jpanel = new JPanel();
        myPanel = jpanel;
        jpanel.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1, false, false));
        JCheckBox jcheckbox = new JCheckBox();
        myUseAuthorFilter = jcheckbox;
        jcheckbox.setText(Messages.getString("TFSVersionFilterComponent.AuthorCheckboxText"));
        jpanel.add(jcheckbox, new GridConstraints(0, 0, 1, 1, 8, 0, 3, 0, null, null, null));
        JTextField jtextfield = new JTextField();
        myAuthorField = jtextfield;
        jpanel.add(jtextfield, new GridConstraints(0, 1, 1, 1, 8, 1, 6, 0, null, new Dimension(150, -1), null));
        JPanel jpanel1 = new JPanel();
        myStandardPanel = jpanel1;
        jpanel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1, false, false));
        jpanel.add(jpanel1, new GridConstraints(1, 0, 1, 2, 0, 3, 3, 3, null, null, null));
    }
}
